﻿using Leo.Common.MQ;

namespace Leo.Common.RabbitMQ
{
    internal class HandlerDescriptor
    {

        public HandlerDescriptor(Type handlerType, object handlerInstance)
        {
            this.CheckInputParameter(handlerType, handlerInstance);
            this.HandlerType = handlerType;
            this.HandlerInstance = handlerInstance;
            if (this.HandlerType == null)
            {
                this.HandlerType = handlerInstance.GetType();
            }
        }


        private void CheckInputParameter(Type handlerType, object handlerInstance)
        {
            if (handlerType == null && handlerInstance == null)
            {
                //throw new JaredsException("类型和实例至少要传入一个，不能同时为空");
                throw new Exception("类型和实例至少要传入一个，不能同时为空");
            }
            if (handlerInstance != null && !(handlerInstance is IMessageHandler))
            {
                //throw new JaredsException("指定的实例未实现IMessageHandler接口");
                throw new Exception("指定的实例未实现IMessageHandler接口");
            }
            if (handlerType != null && !typeof(IMessageHandler).IsAssignableFrom(handlerType))
            {
                //throw new JaredsException("指定的类型未实现IMessageHandler接口");
                throw new Exception("指定的类型未实现IMessageHandler接口");
            }
        }


        public Type HandlerType { get; set; }

        public object HandlerInstance { get; set; }

        public override bool Equals(object obj)
        {
            if (!object.Equals(obj, null))
            {
                HandlerDescriptor handlerDescriptor = obj as HandlerDescriptor;
                if (handlerDescriptor != null)
                {
                    bool flag = handlerDescriptor.HandlerType == this.HandlerType;
                    bool flag2 = handlerDescriptor.HandlerInstance == this.HandlerInstance;
                    return flag && flag2;
                }
            }
            return false;
        }

        public override int GetHashCode()
        {
            return this.HandlerType.GetHashCode();
        }

        public static bool operator ==(HandlerDescriptor left, HandlerDescriptor right)
        {
            bool flag = object.Equals(left, null);
            bool flag2 = object.Equals(right, null);
            return (flag && flag2) || (!flag && !flag2 && left.Equals(right));
        }

        public static bool operator !=(HandlerDescriptor left, HandlerDescriptor right)
        {
            return !(left == right);
        }
    }
}
